package com.example.librarysystem.Controller;

import com.example.librarysystem.Service.*;
import com.example.librarysystem.entity.Borrow;
import com.example.librarysystem.entity.Credit;
import com.example.librarysystem.entity.Order;
import com.example.librarysystem.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Api(value = "定时器接口")
public class SchedulingTask {
    @Autowired
    private BooksService booksService;
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private StarService starService;
    @Autowired
    private BookManagementService bookManagementService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BorrowerService borrowerService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CreditService creditService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MailService mailService;

    @ApiOperation(value = "定时任务每天8点")
    @Scheduled(cron = "0 0 8 * * ?")
    public void execute() {
        //System.out.println("hh");
        borrowScan();
        orderScan();
        scanCredit();
    }

    private void borrowScan(){
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 15);
        Date banDate = ca.getTime();
        ca.add(Calendar.DATE, -20);// num为增加的天数，可以改变的
        Date start = ca.getTime();
        Date now = new Date();
        Calendar temp = Calendar.getInstance();
        System.out.println(start);
        List<Borrow> borrows = borrowService.selectBorrowerBetween(new java.sql.Date(start.getTime()),new java.sql.Date(now.getTime()));
        for (Borrow borrow : borrows){
            String mail = borrowerService.selectById(borrow.getBorrowerId()).getEmail();
            mailService.sendOverMail(mail,borrow.getEnd(),borrow.getBookName(),borrow.getBorrowerName());
        }
        borrows = borrowService.selectBorrowerOver(new java.sql.Date(start.getTime()));
        for (Borrow borrow : borrows){
            Integer borrowerId = borrow.getBorrowerId();
            String mail = borrowerService.selectById(borrow.getBorrowerId()).getEmail();
            mailService.sendBanMail(mail,borrow.getStart(), borrow.getEnd(),borrow.getBookName(),borrow.getBorrowerName(),0);
            Credit credit = creditService.selectByBorrowerIdWithBreach(borrowerId);
            if(credit == null){
                credit = new Credit();
                credit.setBorrowerId(borrowerId);
                credit.setBorrowerName(borrowerService.selectById(borrowerId).getName());
                credit.setBreachStart(new java.sql.Date(now.getTime()));
                credit.setBreachEnd(new java.sql.Date(banDate.getTime()));
                credit.setIsBreach(true);
                creditService.insertCredit(credit);
            }
            temp.setTime(credit.getBreachEnd());
            temp.add(Calendar.DATE,15);
            credit.setBreachEnd(new java.sql.Date(temp.getTime().getTime()));
            creditService.updateCredit(credit);
        }
        ca.add(Calendar.DATE, 10);// num为增加的天数，可以改变的
        start = ca.getTime();
        borrows = borrowService.selectBorrowerBetween(new java.sql.Date(now.getTime()),new java.sql.Date(start.getTime()));
        for (Borrow borrow : borrows){
            String mail = borrowerService.selectById(borrow.getBorrowerId()).getEmail();
            mailService.sendReturnMail(mail,borrow.getEnd(),borrow.getBookName(),borrow.getBorrowerName());
        }
    }
    private void orderScan(){
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 15);
        Date banDate = ca.getTime();
        ca.add(Calendar.DATE, -10);// num为增加的天数，可以改变的
        Date start = ca.getTime();
        Date now = new Date();
        Calendar temp = Calendar.getInstance();
        List<Order> orders = orderService.selectOrderBetween(new java.sql.Date(now.getTime()),new java.sql.Date(start.getTime()));
        for (Order order: orders){
            String mail = borrowerService.selectById(order.getBorrowerId()).getEmail();
            mailService.sendOrderMail(mail,order.getStartDate(),order.getEndDate(), order.getBookName(),order.getBorrowerName());
        }
        orders = orderService.selectOrderOver(new java.sql.Date(now.getTime()));
        for (Order order: orders){
            Integer borrowerId = order.getBorrowerId();
            String mail = borrowerService.selectById(order.getBorrowerId()).getEmail();
            mailService.sendBanMail(mail,order.getStartDate(), order.getEndDate(),order.getBookName(),order.getBorrowerName(),0);
            Credit credit = creditService.selectByBorrowerIdWithBreach(borrowerId);
            if(credit == null){
                credit = new Credit();
                credit.setBorrowerId(borrowerId);
                credit.setBorrowerName(borrowerService.selectById(borrowerId).getName());
                credit.setBreachStart(new java.sql.Date(now.getTime()));
                credit.setBreachEnd(new java.sql.Date(banDate.getTime()));
                credit.setIsBreach(true);
                creditService.insertCredit(credit);
            }
            temp.setTime(credit.getBreachEnd());
            temp.add(Calendar.DATE,15);
            credit.setBreachEnd(new java.sql.Date(temp.getTime().getTime()));
            creditService.updateCredit(credit);
        }
    }
    private void scanCredit(){
        Date now = new Date();
        java.sql.Date nowSql = new java.sql.Date(now.getTime());
        List<Credit> credits = creditService.selectByEndDate(nowSql);
        for (Credit credit: credits){
            credit.setIsBreach(false);
            creditService.updateCredit(credit);
        }
    }
}
